#include "sessiondetailwidget.h"
#include <QVBoxLayout>
#include <QFontMetrics>
#include <QMessageBox>

#define TEST_UI 1

AvatarItem::AvatarItem(const QIcon &avatar, const QString &name)
{
    // 1. 设置自身属性
    this->setFixedSize(70, 80);
    // 2. 创建布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignHCenter);
    this->setLayout(layout);
    // 3. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(45, 45);
    avatarBtn->setIconSize(QSize(45, 45));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton { border: none; }");
    // 4. 创建名字
    nameLabel = new QLabel();
    nameLabel->setText(name);
    QFont font("微软雅黑", 12);
    nameLabel->setFont(font);
    nameLabel->setAlignment(Qt::AlignCenter);

    // 5. 如果名称过长，进行截断
    const int MAX_WIDTH = 65; // 最大名称长度
    QFontMetrics matrics(font);
    int totalWidth = matrics.horizontalAdvance(name); // 获取名称总长度
    if(totalWidth >= MAX_WIDTH) {
        QString tail = "...";
        int tailWidth = matrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth; // 显示的内容长度
        int availableSize = name.size() * ((double)availableWidth / totalWidth);

        QString newName = name.left(availableSize);
        nameLabel->setText(newName + tail); // 设置显示内容
    }

    // 6. 添加到布局
    layout->addWidget(avatarBtn);
    layout->addWidget(nameLabel);
}

SessionDetailWidget::SessionDetailWidget(QWidget *parent, const UserInfo &userinfo)
{
    // 1. 设置基本属性
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/res/image/chat.png"));
    this->setFixedSize(300, 300);
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 0, 50, 0); // left top right bottom
    this->setLayout(layout);
    // 3. 添加 "创建群" 控件
    AvatarItem* createGroupBtn = new AvatarItem(QIcon("://res/image/create.png"), "添加");
    layout->addWidget(createGroupBtn, 0, 0);
    // 4. 添加用户信息
#if TEST_UI
    AvatarItem* curUser = new AvatarItem(QIcon(":/res/image/default_avatar.png"), "李田所");
    layout->addWidget(curUser);
#else
    AvatarItem* curUser = new AvatarItem(userinfo.avatar, userinfo.nickname);
    layout->addWidget(curUser);
#endif
    // 5. 添加 DeleteButton
    delFriendBtn = new QPushButton();
    delFriendBtn->setFixedHeight(50);
    delFriendBtn->setText("删除好友");
    QString style = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; } ";
    style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    delFriendBtn->setStyleSheet(style);
    layout->addWidget(delFriendBtn, 1, 0, 1, 3);

    // 6. 连接信号槽
    // connect(createGroupBtn->getAvatar(), &QPushButton::clicked, this, [=]() {
    //     ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this, userInfo.userId);
    //     chooseFriendDialog->exec();
    // });
    connect(delFriendBtn, &QPushButton::clicked, this, &SessionDetailWidget::clickedDelFriendBtn);
}

void SessionDetailWidget::clickedDelFriendBtn()
{
    // 1. 弹出messagebox对话框
    auto result = QMessageBox::warning(this, "确认删除", "确认删除该好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG_INFO() << "用户取消了好友删除";
        return;
    }

    // 2. 发送请求
    LOG_DEBUG() << "删除操作 TODO...";

    // 3. 关闭当前窗口
    this->close();
}
